package com.example.poverty.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.poverty.entity.Village;
import com.example.poverty.service.VillageService;
import com.example.poverty.utils.Const;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 前端 村户 控制类
 */
@RestController
@RequestMapping("/village")
public class VillageController {

    @Autowired
    private VillageService villageService;

    /**
     * 添加村户信息
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Object addVillage(HttpServletRequest request) {
        JSONObject jsonObject = new JSONObject();
        String townsId = request.getParameter("townsId").trim();  //村户id
        String name = request.getParameter("name").trim();  //  户主姓名
        String familyCategory = request.getParameter("familyCategory").trim();  //家庭类别
        String phone = request.getParameter("phone").trim();  // 电话
        String housingSituation = request.getParameter("housingSituation").trim();  //住房情况
        String familyNum = request.getParameter("familyNum").trim();  // 家庭人数
        String keyPersonnel = request.getParameter("keyPersonnel").trim();  // 重点人员
        String township = request.getParameter("township").trim();  //直属乡镇

        Village village = new Village();
        village.setTownsId(Integer.parseInt(townsId));
        village.setName(name);
        village.setFamilyCategory(new Byte(familyCategory));
        village.setPhone(phone);
        village.setHousingSituation(housingSituation);
        village.setFamilyNum(Integer.parseInt(familyNum));
        village.setKeyPersonnel(new Byte(keyPersonnel));
        village.setTownship(township);
        boolean flag = villageService.insert(village);
        if (flag) {
            jsonObject.put(Const.CODE, 1);
            jsonObject.put(Const.MSG, "添加成功");
            return jsonObject;
        }
        jsonObject.put(Const.CODE, 0);
        jsonObject.put(Const.MSG, "添加失败");
        return jsonObject;
    }

    /**
     * 修改村户信息
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Object updateVillage(HttpServletRequest request) {
        JSONObject jsonObject = new JSONObject();
        String id = request.getParameter("id").trim();  // 主键
        String name = request.getParameter("name").trim();  //  户主姓名
        String familyCategory = request.getParameter("familyCategory").trim();  //家庭类别
        String phone = request.getParameter("phone").trim();  // 电话
        String housingSituation = request.getParameter("housingSituation").trim();  //住房情况
        String familyNum = request.getParameter("familyNum").trim();  // 家庭人数
        String keyPersonnel = request.getParameter("keyPersonnel").trim();  // 重点人员
        String township = request.getParameter("township").trim();  //直属乡镇

        Village village = new Village();
        village.setId(Integer.parseInt(id));
        village.setName(name);
        village.setFamilyCategory(new Byte(familyCategory));
        village.setPhone(phone);
        village.setHousingSituation(housingSituation);
        village.setFamilyNum(Integer.parseInt(familyNum));
        village.setKeyPersonnel(new Byte(keyPersonnel));
        village.setTownship(township);
        boolean flag = villageService.update(village);
        if (flag) {
            jsonObject.put(Const.CODE, 1);
            jsonObject.put(Const.MSG, "修改成功");
            return jsonObject;
        }
        jsonObject.put(Const.CODE, 0);
        jsonObject.put(Const.MSG, "修改失败");
        return jsonObject;
    }

    /**
     * 删除村户信息
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public Object deleteVillage(HttpServletRequest request) {
        String id = request.getParameter("id").trim();  // 主键
        boolean flag = villageService.delete(Integer.parseInt(id));
        return flag;
    }

    /**
     * 根据主键查询整个村户的信息
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/selectByPrimaryKey", method = RequestMethod.GET)
    public Object selectByPrimaryKey(HttpServletRequest request) {
        String id = request.getParameter("id").trim();
        return villageService.selectByPrimaryKey(Integer.parseInt(id));
    }

    /**
     * 查询所有村户信息
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/allVillage", method = RequestMethod.GET)
    public Object allVillage(HttpServletRequest request) {
        return villageService.allVillage();
    }

    /**
     * 根据户主名查询户主信息
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/getName", method = RequestMethod.GET)
    public Object getName(HttpServletRequest request) {
        String name = request.getParameter("name").trim();
        return villageService.getName(name);
    }

    /**
     * 根据村镇id查询村户信息
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/villageOfTownsId", method = RequestMethod.GET)
    public Object villageOfTownsId(HttpServletRequest request) {
        String townsId = request.getParameter("townsId").trim(); //村镇id
        return villageService.villageOfTownsId(Integer.parseInt(townsId));
    }

}
